Imports HelpServiceTypeLib

Module RemoteAssist

    Public Function makeRaTicket(ByVal strUser As String, ByVal strComputer As String, ByRef strSaveLocation As String) As Boolean
        Try
            Dim hs As New HelpServicesClasses
            'Dim sessions As Session() = hs.GetSessions(strComputer)
            Dim strUserName As String
            Dim strUserDomain As String
            Dim strTicket As String = String.Empty
            Dim strSessionHelpBlob As String = String.Empty
            Dim xml As System.Xml.XmlDocument

            'replace with value from XML config file
            strSaveLocation = My.Computer.FileSystem.SpecialDirectories.Temp & "\" & _
                              strComputer & "_" & getFormattedFileDateTime() & "_" & _
                              "RAInvite" & ".msrcincident"

            strUserDomain = strUser.Remove(strUser.IndexOf("\"), strUser.Length - strUser.IndexOf("\"))
            strUserName = strUser.Remove(0, strUser.IndexOf("\") + 1)
            strTicket = hs.GenerateTicket(strComputer, _
                            strUserName, _
                            strUserDomain, _
                            0, _
                            strSessionHelpBlob)
            xml = hs.FormatIncident(strTicket, 60)
            xml.Save(strSaveLocation)
        Catch notAuth As UnauthorizedAccessException
            MessageBox.Show("An error occured while Remote Assistance was connecting to the computer " & strComputer & "." & vbCrLf & "Access Denied.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        Catch comErr As System.Runtime.InteropServices.COMException
            MessageBox.Show(comErr.Message & vbCrLf & "Error number: " & Hex(comErr.ErrorCode) & _
                            vbCrLf & comErr.StackTrace.ToString)
            '800706ba = The RPC server is unavailable
            Return False
        End Try
        Return True
    End Function

    Private Function getFormattedFileDateTime() As String
        Dim timeNow As DateTime = DateTime.Now
        Dim year As Integer = timeNow.Year
        Dim sYear As String = String.Empty
        Dim month As Integer = timeNow.Month
        Dim sMonth As String = String.Empty
        Dim day As Integer = timeNow.Day
        Dim sDay As String = String.Empty
        Dim hour As Integer = timeNow.Hour
        Dim sHour As String = String.Empty
        Dim minute As Integer = timeNow.Minute
        Dim sMinute As String = String.Empty

        If year < 10 Then
            sYear = 0 & year.ToString
        Else
            sYear = year.ToString
        End If
        If month < 10 Then
            sMonth = 0 & month.ToString
        Else
            sMonth = month.ToString
        End If
        If day < 10 Then
            sDay = 0 & day.ToString
        Else
            sDay = day.ToString
        End If
        If hour < 10 Then
            sHour = 0 & hour.ToString
        Else
            sHour = hour.ToString
        End If
        If minute < 10 Then
            sMinute = 0 & minute.ToString
        Else
            sMinute = minute.ToString
        End If

        Dim formattedDate As String = sYear & sMonth & sDay & "_" & sHour & sMinute
        Return formattedDate
    End Function

End Module
